package com.ctshk.rpc.order.tour.service.impl;

import java.time.LocalDateTime;
import java.util.List;

import com.ctshk.common.utils.SnowflakeIdWorker;
import org.apache.dubbo.config.annotation.DubboService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ctshk.common.dto.Result;
import com.ctshk.common.model.TokenUser;
import com.ctshk.common.service.support.BaseService;
import com.ctshk.common.utils.EntityUtil;
import com.ctshk.rpc.order.tour.code.ConstantsCode;
import com.ctshk.rpc.order.tour.code.IsDeletedCode;
import com.ctshk.rpc.order.tour.code.SeriesOrderTouristOtherFeeFeeTypeCode;
import com.ctshk.rpc.order.tour.entity.SeriesOrderTouristOtherFee;
import com.ctshk.rpc.order.tour.mapper.SeriesOrderTouristOtherFeeMapper;
import com.ctshk.rpc.order.tour.req.SeriesOrderTouristOtherFeeAddReq;
import com.ctshk.rpc.order.tour.req.SeriesOrderTouristOtherFeeSaveBatchReq;
import com.ctshk.rpc.order.tour.req.SeriesOrderTouristOtherFeeSaveRefundReq;
import com.ctshk.rpc.order.tour.service.ISeriesOrderTouristOtherFeeService;

/**
 * <p>
 * 系列团游客其他费用 服务实现类
 * </p>
 *
 * @author 谭响
 * @since 2021-01-22
 */
@DubboService
public class SeriesOrderTouristOtherFeeServiceImpl extends ServiceImpl<SeriesOrderTouristOtherFeeMapper, SeriesOrderTouristOtherFee> implements ISeriesOrderTouristOtherFeeService, BaseService {
	
	@Autowired
	SeriesOrderTouristOtherFeeMapper seriesOrderTouristOtherFeeMapper;

    /**
     * 添加
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public Result add(SeriesOrderTouristOtherFeeAddReq req) {
        SeriesOrderTouristOtherFee seriesOrderTouristOtherFee = EntityUtil.copy(req, SeriesOrderTouristOtherFee.class);
        LocalDateTime now = LocalDateTime.now();
        seriesOrderTouristOtherFee.setFeeType(SeriesOrderTouristOtherFeeFeeTypeCode.OTHER.getCode());
        seriesOrderTouristOtherFee.setGmtCreate(now);
        seriesOrderTouristOtherFee.setGmtModified(now);
        seriesOrderTouristOtherFee.setCreateId(req.getUserId());
        seriesOrderTouristOtherFee.setModifiedId(req.getUserId());
        seriesOrderTouristOtherFee.setIsDeleted(IsDeletedCode.NO.getCode());
        save(seriesOrderTouristOtherFee);
        return Result.success();
    }

    /**
     * 插入批量
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public Boolean saveBatch(List<SeriesOrderTouristOtherFeeSaveBatchReq> listReq) {
        List<SeriesOrderTouristOtherFee> touristInfoList = EntityUtil.copyList(listReq, SeriesOrderTouristOtherFee.class);
        return super.saveBatch(touristInfoList);
    }

    /**
     * 更新旅客其他费用-退款
     */
    @Transactional(rollbackFor = Exception.class)
    @Override
    public boolean saveRefund(SeriesOrderTouristOtherFeeSaveRefundReq req, TokenUser tokenUser) {
    	/*SeriesOrderTouristOtherFee otherFee = getBaseMapper().findRefundByTourist(req.getTouristId());
    	if (otherFee == null) {
    		otherFee = new SeriesOrderTouristOtherFee();
            otherFee.setId(SnowflakeIdWorker.nextId());
    	}*/
        SeriesOrderTouristOtherFee otherFee = new SeriesOrderTouristOtherFee();
        otherFee.setId(SnowflakeIdWorker.nextId());
        otherFee.setFeeType(SeriesOrderTouristOtherFeeFeeTypeCode.OTHER.getCode());
        otherFee.setOrderId(req.getOrderId());
        otherFee.setTouristId(req.getTouristId());
        otherFee.setFeeName(ConstantsCode.REFUND.getMsg());
        otherFee.setRelationId(req.getRelationId());
        otherFee.setAmount(req.getAmount());
        LocalDateTime now = LocalDateTime.now();
        otherFee.setGmtModified(now);
        otherFee.setModifiedId(tokenUser.getId());
        otherFee.setIsDeleted(IsDeletedCode.NO.getCode());
        return save(otherFee);
    }
    
    /**
     * 删除某旅客的代收费用
     * @param touristId
     * @param feeType
     * @param userId
     */
    @Transactional
    @Override
    public void deleteCollectionByTourist(Long touristId, Long userId) {
    	validateNotNull(touristId);
    	
    	SeriesOrderTouristOtherFee entity = new SeriesOrderTouristOtherFee();
    	entity.setModifiedId(userId);
    	entity.setGmtModified(LocalDateTime.now());
    	entity.setIsDeleted(IsDeletedCode.YES.getCode());
    	seriesOrderTouristOtherFeeMapper.updateByKey(entity, SeriesOrderTouristOtherFee::getTouristId, touristId
    			, SeriesOrderTouristOtherFee::getFeeType, SeriesOrderTouristOtherFeeFeeTypeCode.COLLECTION.getCode());
    }
}
